package com.qfedu.edu.iotserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.edu.dto.PageDto;
import com.qfedu.edu.pojo.Area;
import com.qfedu.edu.iotserver.mapper.AreaMapper;
import com.qfedu.edu.iotserver.service.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.edu.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 行政区划表 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2024-09-13
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 分页获取行政区划列表
     * @return
     */
    @Override
    public PageResult<Area> getAreaList(PageDto pageDto, String name, String code) {
        int page = pageDto.getPage();       // 获取当前页码
        int pageSize = pageDto.getPageSize(); // 获取每页条目数
        Page<Area> areaPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Area> queryWrapper = new LambdaQueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.eq(Area::getName, name);
        }
        if (code != null && !code.isEmpty()) {
            queryWrapper.eq(Area::getCode, code);
        }
        IPage<Area> paginatedResult = areaMapper.selectPage(areaPage, queryWrapper);
        return new PageResult<Area>(paginatedResult.getTotal(), paginatedResult.getRecords());
    }
}
